let winObjIndex = {};

function waitObj(winObj) {
  return new Promise(function (resolve) {
    return setTimeout(() => {
      if (window[winObj]) {
        resolve(window[winObj]);
        return;
      }
      winObjIndex[winObj] = (winObjIndex[winObj] || 0) + 1;
      console.log(
        `动态加载对象[{}], 第{}次尝试...`,
        winObj,
        winObjIndex[winObj]
      );
      if (winObjIndex[winObj] > 4) {
        console.log("等待对象加载失败，可能是因为网络超时-", winObj);
        return;
      }
      waitObj(winObj).then(() => resolve(window[winObj]));
    }, 1000);
  });
}

function wait(fun) {
  return new Promise(function (resolve) {
    return setTimeout(() => {
      if (fun()) {
        resolve();
        return;
      }
      wait(fun).then(() => resolve());
    }, 1000);
  });
}

const UtilWaitObj = {
  waitObj,
  wait,
};

export default UtilWaitObj;
